function varargout = das_gui(varargin)
% DAS_GUI main GUI of DASMiner Browser 
%      DAS_GUI, by itself, creates a new DAS_GUI or raises the existing
%      singleton*.
%
%      H = DAS_GUI returns the handle to a new DAS_GUI or the handle to
%      the existing singleton*.
%
%      DAS_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DAS_GUI.M with the given input arguments.
%
%      DAS_GUI('Property','Value',...) creates a new DAS_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before das_gui_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to das_gui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: SEARCHREGISTRY_GUI
% Last Modified by GUIDE v2.5 03-Sep-2009 14:57:14
%
%    DASMiner: DAS library and browser for Matlab.
%    Diogo Veiga, March 2008.

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @das_gui_OpeningFcn, ...
                   'gui_OutputFcn',  @das_gui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end

% End initialization code - DO NOT EDIT


% --- Executes just before das_gui is made visible.
function das_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to das_gui (see VARARGIN)

% Choose default command line output for das_gui
handles.output = hObject;

timeout = get(handles.field_timeout,'String');
handles.timeout = str2double(timeout);

resizeButtonImage(handles.pushbutton_infoCS,16,16);
resizeButtonImage(handles.pushbutton_infoCS2,16,16);
resizeButtonImage(handles.pushbutton_ClearDisplay,32,32);
resizeButtonImage(handles.pushbutton_infoServer,32,32);
resizeButtonImage(handles.pushbutton_Reset,48,50);
resizeButtonImage(handles.pushbutton_Query,48,50);
resizeButtonImage(handles.pushbutton_SendBrowser,32,32);
resizeButtonImage(handles.pushbutton_SendXMLFile,32,32);
resizeButtonImage(handles.pushbutton_getURL,32,32);


set(handles.text_ServerDesc,'Units','characters');
pos = get(handles.text_ServerDesc,'Position');
set(handles.text_ServerDesc,'Position',[pos(1) pos(2) pos(3) 2]); %set original pixel size

% Update handles structure
guidata(hObject, handles);
% UIWAIT makes das_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = das_gui_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1


% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on selection change in listbox2.
function listbox2_Callback(hObject, eventdata, handles)
% hObject    handle to listbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listbox2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox2


% --- Executes during object creation, after setting all properties.
function listbox2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in listbox_Links.
function listbox_Links_Callback(hObject, eventdata, handles)
% hObject    handle to listbox_Links (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listbox_Links contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox_Links
str = get(hObject, 'String');
val = get(hObject,'Value');
switch str{val};
    case 'Biodas.org'
        web('http://www.biodas.org/', '-browser');
    case 'DAS/1.53 Specification'
        web('http://www.biodas.org/documents/spec.html', '-browser');
end

% --- Executes during object creation, after setting all properties.
function listbox_Links_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox_Links (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu_Server.
function popupmenu_Server_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_Server (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu_Server contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_Server

resetFeatPanel(handles);
resetSegmPanel(handles);
resetXMLDispPanel(handles);
resetServerPanel(handles);

str = get(hObject, 'String');
val = get(hObject,'Value');
switch str{val};
    case 'Choose Data Server ...'
        handles.baseUrl = '';
    case 'Add Data Server ...'
        hGUI = searchRegistry_gui(handles.popupmenu_Server);
        handlesRegGUI = guidata(hGUI);
        handles.handlesRegGUI = handlesRegGUI;
    otherwise
        server = str{val};
        strServer = load('localServ.mat', 'locServ');
        locServ = strServer.locServ;
        
        for i=1:size(locServ,2)
           if (strcmp(locServ(i).title,server))
              handles.baseUrl = locServ(i).baseUrl;
              handles.dsn = locServ(i).dsn;
              handles.serverURI = locServ(i).uri;
              handles.serverCoordinates = locServ(i).coordinates;
              handles.serverCoordinatesURI = locServ(i).coordinatesURI;
              %Populates Data Source Menu
              set(handles.popupmenu_DSN,'String',locServ(i).dsn);
              set(handles.popupmenu_DSN,'Value',1);
              
              %Populates Data Source Menu
              handles.serverCapab = locServ(i).capabilities;
              set(handles.popupmenu_Comm,'String',['Choose Command ...' locServ(i).capabilities]);
              set(handles.popupmenu_Comm,'Value',1);
              set(handles.popupmenu_Comm,'Visible','on');

              set(handles.pushbutton_infoServer,'Visible','on');
              set(handles.text_ServerDesc,'String',locServ(i).desc);
              set(handles.text_ServerDesc,'Visible','on');
              break;
           end
        end
        
end
guidata(hObject,handles);

% %Populates Data Source Menu
% switch str{val};
%     case 'Choose Data Server ...'
%         handles.baseUrl = '';
%         set(handles.popupmenu_DSN,'Visible','off');
%         set(handles.popupmenu_Comm,'Visible','off');
%     case 'Add Data Server ...'
%         
%     otherwise
%         hDial = openRunningDialog;
%         [xmlS, xml, status] = executeDASCommand(handles.baseUrl,'command','dsn','timeout',handles.timeout);
%         delete(hDial);
%         clear hDial;
%         
%         if (~checkDASCommand(xmlS,status))
%             return;
%         end
% 
%         dsn = parseDataSources(xml);
%         handles.dsnStr = dsn;
%         guidata(hObject,handles);
%         nDSN = numel(dsn);
%         strOptions = 'Choose Data Source ...';
% 
%         for i=1:nDSN
%             strOptions = strvcat(strOptions, dsn(i).id);
%         end
%         set(handles.popupmenu_DSN,'String',cellstr(strOptions));
%         set(handles.popupmenu_DSN,'Value',1);
%         set(handles.popupmenu_DSN,'Visible','on');
% 
% end




% --- Executes during object creation, after setting all properties.
function popupmenu_Server_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_Server (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

populateServerMenu(hObject);

% --- Executes on selection change in popupmenu_DSN.
function popupmenu_DSN_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_DSN (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu_DSN contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_DSN

% resetFeatPanel(handles);
% resetSegmPanel(handles);
% resetXMLDispPanel(handles);
% set(handles.popupmenu_Comm,'Value',1);
% 
% str = get(hObject, 'String');
% val = get(hObject,'Value');
% 
% nDSN = numel(handles.dsnStr);
% 
% for i=1:nDSN
%    if (strcmp(handles.dsnStr(i).id,str{val}))
%        handles.mapmaster = handles.dsnStr(i).id;
%        handles.dsn = str{val};
%        guidata(hObject,handles);
%    end
% end
% 
% set(handles.popupmenu_Comm,'Visible','on');

% --- Executes during object creation, after setting all properties.
function popupmenu_DSN_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_DSN (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu_SegDef.
function popupmenu_SegDef_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_SegDef (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu_SegDef contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_SegDef
contents = get(hObject, 'String');
val = get(hObject,'Value');

resetIDdefPanel(handles);
resetDNAdefPanel(handles);

commStr = get(handles.popupmenu_Comm,'String');


if (any(strcmpi(contents{val},{'Chromosome','Clone', 'Contig', 'NT_Contig', 'Scaffold'})) ) %open DNA definitions panel
    set(handles.text16,'String',contents{val});
    configureInfoCSButton(handles,'DNA Panel');
    set(handles.uipanel_IDdefinition,'Visible','off');
    set(handles.uipanel_DNAdefinitions,'Visible','on');
    if(strcmp(commStr{get(handles.popupmenu_Comm,'Value')},'features'))
        set(handles.popupmenu_FeatOptions2,'Visible','on');
    end
elseif (any(strcmpi(contents{val},{'Protein Sequence','Protein Structure', 'Volume Map','Gene_ID'})) ) %open ID definitions panel
    configureInfoCSButton(handles,'ID Panel');
    set(handles.uipanel_DNAdefinitions,'Visible','off');
    set(handles.uipanel_IDdefinition,'Visible','on');
    if(strcmp(commStr{get(handles.popupmenu_Comm,'Value')},'features'))
        set(handles.popupmenu_FeatOptions,'Visible','on');
    end
elseif (strcmp(contents{val},'Choose data type...'))
    set(handles.uipanel_DNAdefinitions,'Visible','off');
    set(handles.uipanel_IDdefinition,'Visible','off');
end


% --- Executes during object creation, after setting all properties.
function popupmenu_SegDef_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_SegDef (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end





function field_Chr_Callback(hObject, eventdata, handles)
% hObject    handle to field_Chr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of field_Chr as text
%        str2double(get(hObject,'String')) returns contents of field_Chr as a double


% --- Executes during object creation, after setting all properties.
function field_Chr_CreateFcn(hObject, eventdata, handles)
% hObject    handle to field_Chr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function field_Start_Callback(hObject, eventdata, handles)
% hObject    handle to field_Start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of field_Start as text
%        str2double(get(hObject,'String')) returns contents of field_Start as a double


% --- Executes during object creation, after setting all properties.
function field_Start_CreateFcn(hObject, eventdata, handles)
% hObject    handle to field_Start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function field_Stop_Callback(hObject, eventdata, handles)
% hObject    handle to field_Stop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of field_Stop as text
%        str2double(get(hObject,'String')) returns contents of field_Stop as a double


% --- Executes during object creation, after setting all properties.
function field_Stop_CreateFcn(hObject, eventdata, handles)
% hObject    handle to field_Stop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function field_ID_Callback(hObject, eventdata, handles)
% hObject    handle to field_ID (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of field_ID as text
%        str2double(get(hObject,'String')) returns contents of field_ID as a double
handles.ID = get(hObject,'String');
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function field_ID_CreateFcn(hObject, eventdata, handles)
% hObject    handle to field_ID (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in listbox_Feat.
function listbox_Feat_Callback(hObject, eventdata, handles)
% hObject    handle to listbox_Feat (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listbox_Feat contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox_Feat

% --- Executes during object creation, after setting all properties.
function listbox_Feat_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox_Feat (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton_Query.
function pushbutton_Query_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_Query (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if (get(handles.popupmenu_Server,'Value') == 1)
    msgbox('Select a DAS Server in the menu.','Data Server Requested','warn');
    return
end

if (get(handles.popupmenu_Comm,'Value') == 1)
    msgbox('Select a DAS command in the menu.','Command Requested','warn');
    return 
end

str = get(handles.popupmenu_Comm,'String');
val = get(handles.popupmenu_Comm,'Value');
handles.command = str{val};
guidata(hObject,handles);

if (any(strcmpi(handles.command,{'dna', 'sequence', 'types', 'features'})) )
    str = get(handles.popupmenu_SegDef,'String');
    val = get(handles.popupmenu_SegDef,'Value');
    dataType = str{val};
end

if (any(strcmpi(handles.command,{'structure','alignment','interaction'})) )
    if (isempty(get(handles.field_ID,'String')))
        msgbox('Fill out ID','Data Requested','warn');
        return
    end
end


switch handles.command
    case 'structure'
        
        hDial = openRunningDialog;
        [xmlS, xml, status,url,xmlS_2] = executeDasCommand(handles.baseUrl,'command', 'structure', 'DSN', handles.dsn ,'segment',get(handles.field_ID,'String'), ...
            'timeout',handles.timeout);
        delete(hDial);
        clear hDial;
        if (~checkDASCommand(xmlS,status))
            return;
        end
        setResultsFields(hObject,handles,xml,url);
                
    case 'alignment'

        hDial = openRunningDialog;
        [xmlS, xml, status,url,xmlS_2] = executeDasCommand(handles.baseUrl,'command', 'alignment', 'DSN', handles.dsn ,'segment',get(handles.field_ID,'String'), ...
            'timeout',handles.timeout);
        delete(hDial);
        clear hDial;
        if (~checkDASCommand(xmlS,status))
            return;
        end
        setResultsFields(hObject,handles,xml,url);        
        
    case 'interaction'

        hDial = openRunningDialog;
        [xmlS, xml, status,url,xmlS_2] = executeDasCommand(handles.baseUrl,'command', 'interaction', 'DSN', handles.dsn ,'segment',get(handles.field_ID,'String'), ...
            'timeout',handles.timeout);
        delete(hDial);
        clear hDial;
        if (~checkDASCommand(xmlS,status))
            return;
        end
        setResultsFields(hObject,handles,xml,url);
        
    case 'dna'
        if (get(handles.popupmenu_SegDef,'Value') == 1)
            msgbox('Define the segment type.','Segment Type Requested','warn');
            return 
        end
        
        
        if (any(strcmpi(dataType,{'Chromosome','Clone', 'Contig', 'NT_Contig', 'Scaffold'})) ) %DNA segment

            if (isempty(get(handles.field_Chrom,'String')) || isempty(get(handles.field_Start,'String')) ...
                    || isempty(get(handles.field_Stop,'String')) )
                msgbox('Fill out the fields Chromosome, Start, Stop.','Data Requested','warn');
                return
            end
            hDial = openRunningDialog;
            [xmlS, xml, status,url,xmlS_2] = executeDasCommand(handles.baseUrl,'command', 'dna', 'DSN', handles.dsn ,'chrom',get(handles.field_Chrom,'String'), ...
                'start',get(handles.field_Start,'String'), 'stop', get(handles.field_Stop,'String'), 'timeout',handles.timeout);
            delete(hDial);
            clear hDial;
            if (~checkDASCommand(xmlS,status))
                return;
            end
            setResultsFields(hObject,handles,xml,url);    
            
        else %ID definition

            if (isempty(get(handles.field_ID,'String')))
                msgbox('Fill out ID','Data Requested','warn');
                return
            end
            hDial = openRunningDialog;
            [xmlS, xml, status,url,xmlS_2] = executeDasCommand(handles.baseUrl,'command', 'dna', 'DSN', handles.dsn ,'segment',get(handles.field_ID,'String'), ...
                     'timeout',handles.timeout);
            delete(hDial);
            clear hDial;
            if (~checkDASCommand(xmlS,status))
                return;
            end
            setResultsFields(hObject,handles,xml,url);
        end
        
    case 'sequence'
        
        if (get(handles.popupmenu_SegDef,'Value') == 1)
            msgbox('Define the segment type.','Segment Type Requested','warn');
            return 
        end
        
       
        if (any(strcmpi(dataType,{'Chromosome','Clone', 'Contig', 'NT_Contig', 'Scaffold'})) ) %DNA segment

            if (isempty(get(handles.field_Chrom,'String')) || isempty(get(handles.field_Start,'String')) ...
                    || isempty(get(handles.field_Stop,'String')) )
                msgbox('Fill out the fields Chromosome, Start, Stop.','Data Requested','warn');
                return
            end
            hDial = openRunningDialog;
            [xmlS, xml, status,url,xmlS_2] = executeDasCommand(handles.baseUrl,'command', 'sequence', 'DSN', handles.dsn ,'chrom',get(handles.field_Chrom,'String'), ...
                'start',get(handles.field_Start,'String'), 'stop', get(handles.field_Stop,'String'), 'timeout',handles.timeout);
            delete(hDial);
            clear hDial;
            
            if (~checkDASCommand(xmlS,status))
                return;
            end
            
            setResultsFields(hObject,handles,xml,url);
            
        else %ID definition

            if (isempty(get(handles.field_ID,'String')))
                msgbox('Fill out ID','Data Requested','warn');
                return
            end
            hDial = openRunningDialog;
            [xmlS, xml, status,url,xmlS_2] = executeDasCommand(handles.baseUrl,'command', 'sequence', 'DSN', handles.dsn ,'segment',get(handles.field_ID,'String'), ...
                'timeout',handles.timeout);

            delete(hDial);
            clear hDial;
            
            if (~checkDASCommand(xmlS,status))
                return;
            end

           setResultsFields(hObject,handles,xml,url);

        end
        
    case 'types'
        if (get(handles.popupmenu_SegDef,'Value') == 1)
            msgbox('Define the segment type.','Segment Type Requested','warn');
            return 
        end
        
       
        if (any(strcmpi(dataType,{'Chromosome','Clone', 'Contig', 'NT_Contig', 'Scaffold'})) ) %DNA segment
            
            if (isempty(get(handles.field_Chrom,'String')) || isempty(get(handles.field_Start,'String')) ...
                    || isempty(get(handles.field_Stop,'String')) )
                msgbox('Fill out the fields Chromosome, Start, Stop.','Data Requested','warn');
                return
            end
            hDial = openRunningDialog;
            [xmlS, xml, status,url,xmlS_2] = executeDasCommand(handles.baseUrl,'command', 'types', 'DSN', handles.dsn ,'chrom',get(handles.field_Chrom,'String'), ...
                'start',get(handles.field_Start,'String'), 'stop', get(handles.field_Stop,'String'), 'timeout',handles.timeout);

            delete(hDial);
            clear hDial;
            
            if (~checkDASCommand(xmlS,status))
                return;
            end
            
            setResultsFields(hObject,handles,xml,url);
            
        else %ID definition

            if (isempty(get(handles.field_ID,'String')))
                msgbox('Fill out ID','Data Requested','warn');
                return
            end
            hDial = openRunningDialog;
            [xmlS, xml, status,url,xmlS_2] = executeDasCommand(handles.baseUrl,'command', 'types', 'DSN', handles.dsn ,'segment',get(handles.field_ID,'String'), ...
                'timeout',handles.timeout);
            delete(hDial);
            clear hDial;
            
            if (~checkDASCommand(xmlS,status))
                return;
            end
            
            setResultsFields(hObject,handles,xml,url);
        end
        
    case 'features'
        [xmlS, xml, url, xmlS_2] = queryFeatures(hObject,handles);
    case 'entry_points'
        hDial = openRunningDialog;
        [xmlS, xml, status, url, xmlS_2] = executeDASCommand(handles.baseUrl,'command','entry_points','DSN',handles.dsn, 'timeout',handles.timeout );
        delete(hDial);
        clear hDial;
        
        if (~checkDASCommand(xmlS,status))
            return;
        end
        setResultsFields(hObject,handles,xml,url);
    case 'dsn'
        hDial = openRunningDialog;
        [xmlS, xml, status,url,xmlS_2] = executeDasCommand(handles.baseUrl,'command','dsn','timeout',handles.timeout);
        delete(hDial);
        clear hDial;
        
        if (~checkDASCommand(xmlS,status))
            return;
        end

        setResultsFields(hObject,handles,xml,url);
    case 'stylesheet'
        
        hDial = openRunningDialog;
        [xmlS, xml, status,url,xmlS_2] = executeDasCommand(handles.baseUrl,'command','stylesheet','DSN',handles.dsn, 'timeout',handles.timeout );
        delete(hDial);
        clear hDial;
        
        if (~checkDASCommand(xmlS,status))
            return;
        end

       setResultsFields(hObject,handles,xml,url);

end
assignin('base', 'DASquery_Struct', xmlS);
assignin('base', 'DASquery_Struct_2', xmlS_2);
assignin('base', 'DASquery_url', url);
assignin('base', 'DASquery_XML', xml);
h = msgbox('Query details saved in the workspace','Query Info', 'warn');


% --- Executes on selection change in popupmenu_Comm.
function popupmenu_Comm_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_Comm (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu_Comm contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_Comm

resetFeatPanel(handles);
resetSegmPanel(handles);
resetXMLDispPanel(handles);

str = get(hObject, 'String');
val = get(hObject,'Value');
handles.command = str{val};

if (any(strcmpi(handles.command,{'dna','sequence', 'types', 'features','structure','alignment','interaction'})) ) %open Segment definitions panel
    populateSegDefMenu(handles);
    set(handles.uipanel_Segm,'Visible','on');
end

% --- Executes during object creation, after setting all properties.
function popupmenu_Comm_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_Comm (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function field_XMLDisp_Callback(hObject, eventdata, handles)
% hObject    handle to field_XMLDisp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of field_XMLDisp as text
%        str2double(get(hObject,'String')) returns contents of field_XMLDisp as a double


% --- Executes during object creation, after setting all properties.
function field_XMLDisp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to field_XMLDisp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on slider movement.
function slider3_Callback(hObject, eventdata, handles)
% hObject    handle to slider3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


% --- Executes during object creation, after setting all properties.
function slider3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end




% --- Executes on button press in pushbutton_Reset.
function pushbutton_Reset_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_Reset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

resetFeatPanel(handles);
resetSegmPanel(handles);
resetXMLDispPanel(handles);
resetServerPanel(handles);
set(handles.popupmenu_Server,'Value',1);


% --- Executes on button press in pushbutton_ClearDisplay.
function pushbutton_ClearDisplay_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_ClearDisplay (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
resetXMLDispPanel(handles);




function field_Chrom_Callback(hObject, eventdata, handles)
% hObject    handle to field_Chrom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of field_Chrom as text
%        str2double(get(hObject,'String')) returns contents of field_Chrom as a double


% --- Executes during object creation, after setting all properties.
function field_Chrom_CreateFcn(hObject, eventdata, handles)
% hObject    handle to field_Chrom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit13_Callback(hObject, eventdata, handles)
% hObject    handle to field_Start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of field_Start as text
%        str2double(get(hObject,'String')) returns contents of field_Start as a double


% --- Executes during object creation, after setting all properties.
function edit13_CreateFcn(hObject, eventdata, handles)
% hObject    handle to field_Start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit14_Callback(hObject, eventdata, handles)
% hObject    handle to field_Stop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of field_Stop as text
%        str2double(get(hObject,'String')) returns contents of field_Stop as a double


% --- Executes during object creation, after setting all properties.
function edit14_CreateFcn(hObject, eventdata, handles)
% hObject    handle to field_Stop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on selection change in popupmenu_FeatOptions.
function popupmenu_FeatOptions_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_FeatOptions (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu_FeatOptions contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_FeatOptions

if (get(hObject,'Value') < 3)
    set(handles.uipanel_Feat,'Visible','off');
elseif (get(hObject,'Value') == 3)
    if (populateFeaturesList(handles))
        set(handles.uipanel_Feat,'Visible','on');
    end
end
    


% --- Executes during object creation, after setting all properties.
function popupmenu_FeatOptions_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_FeatOptions (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on selection change in popupmenu_FeatOptions2.
function popupmenu_FeatOptions2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_FeatOptions2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu_FeatOptions2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_FeatOptions2
if (get(hObject,'Value') < 3)
    set(handles.uipanel_Feat,'Visible','off');
elseif (get(hObject,'Value') == 3)
    if (populateFeaturesList(handles))
        set(handles.uipanel_Feat,'Visible','on');
    end
end

% --- Executes during object creation, after setting all properties.
function popupmenu_FeatOptions2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_FeatOptions2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function [status] = populateFeaturesList(handles)

status = 1;
if (get(handles.popupmenu_SegDef,'Value') == 1)
    msgbox('Define the segment type.','Segment Type Requested','warn');
    return 
end

contents = get(handles.popupmenu_SegDef, 'String');
val = get(handles.popupmenu_SegDef,'Value');
       
if (any(strcmpi(contents{val},{'Chromosome','Clone', 'Contig', 'NT_Contig', 'Scaffold'})) ) %DNA definitions panel is selected

    if (isempty(get(handles.field_Chrom,'String')) || isempty(get(handles.field_Start,'String')) ...
            || isempty(get(handles.field_Stop,'String')) )
        msgbox('Fill out the fields Chromosome, Start, Stop.','Data Requested','warn');
        status = 0;
        return
    end
    hDial = openRunningDialog;
    [xmlS, xml, status, url, xmlS_2] = executeDasCommand(handles.baseUrl,'command', 'types', 'DSN', handles.dsn ,'chrom',get(handles.field_Chrom,'String'), ...
        'start',get(handles.field_Start,'String'), 'stop', get(handles.field_Stop,'String'), 'timeout',handles.timeout);
    delete(hDial);
    clear hDial;
    
    if (~checkDASCommand(xmlS,status))
        return;
    end

elseif (any(strcmpi(contents{val},{'Protein Sequence','Protein Structure', 'Volume Map'})) ) %ID definitions panel

    if (isempty(get(handles.field_ID,'String')))
        msgbox('Fill out ID','Data Requested','warn');
        status = 0;
        return
    end
    hDial = openRunningDialog;
    [xmlS, xml, status, url, xmlS_2] = executeDasCommand(handles.baseUrl,'command', 'types', 'DSN', handles.dsn ,'segment',get(handles.field_ID,'String'), ...
        'timeout',handles.timeout);
    delete(hDial);
    clear hDial;
    
    if (~checkDASCommand(xmlS,status))
        return;
    end

end

%Populate features list
features = parseTypes(xml);
nFeatures = numel(features);
strOptions = 'Choose Features ...';

for i=1:nFeatures
    strOptions = strvcat(strOptions, features(i).id);
end
set(handles.listbox_Feat,'String',cellstr(strOptions));
set(handles.listbox_Feat,'Value',1);

function resetXMLDispPanel(handles)
set(handles.field_XMLDisp,'String','');
set(handles.field_queryURL,'String','');

function resetSegmPanel(handles)
set(handles.field_Chrom,'String','');
set(handles.field_ID,'String','');
set(handles.field_Start,'String','');
set(handles.field_Stop,'String','');
set(handles.popupmenu_FeatOptions,'Value',1);
set(handles.popupmenu_FeatOptions2,'Value',1);
set(handles.popupmenu_FeatOptions,'Visible','off');
set(handles.popupmenu_FeatOptions2,'Visible','off');
set(handles.uipanel_DNAdefinitions,'Visible','off');
set(handles.uipanel_IDdefinition,'Visible','off');
set(handles.uipanel_Segm,'Visible','off');
set(handles.popupmenu_SegDef,'String','');

function resetFeatPanel(handles)
set(handles.uipanel_Feat,'Visible','off');
set(handles.listbox_Feat,'String',{'Select Features available ...'});

function resetIDdefPanel(handles)
set(handles.field_ID,'String','');
set(handles.popupmenu_FeatOptions,'Value',1);

function resetDNAdefPanel(handles)
set(handles.popupmenu_FeatOptions2,'Value',1);
set(handles.field_Chrom,'String','');
set(handles.field_Start,'String','');
set(handles.field_Stop,'String','');

function [hDial] = openRunningDialog
hDial = showinfowindow({'Connecting to the server ...','Running request...'},'Please wait ...');
%set(hDial,'CloseRequestFcn','');
t = get(hDial,'Position');
set(hDial,'Position',t)

function [isOkay] = checkDASCommand(xmlS,status)
        
isOkay = 1;
if (~status)
    msgbox('DAS Server is taking too long to respond. Increase timeout and try again.','DAS Server not responding','warn');
    isOkay = 0;
    return;
end

% if (~isstruct(xmlS))
%     msgbox('Problem Parsing XML DAS Response','Bad Parsing','warn');
%     isOkay = 0;
%     return;
% end




function field_timeout_Callback(hObject, eventdata, handles)
% hObject    handle to field_timeout (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of field_timeout as text
%        str2double(get(hObject,'String')) returns contents of field_timeout as a double


% --- Executes during object creation, after setting all properties.
function field_timeout_CreateFcn(hObject, eventdata, handles)
% hObject    handle to field_timeout (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
%movegui(hObject,'center');



% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
closeGUI(hObject,eventdata);

function closeGUI(src,evnt)
%src is the handle of the object generating the callback (the source of the event)
%evnt is the The event data structure (can be empty for some callbacks)
selection = questdlg('Do you want to close DASMiner?',...
                     'Close Request',...
                     'Yes','No','Yes');
switch selection,
   case 'Yes',
    delete(gcf)
   case 'No'
     return
end




% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton_infoServer.
function pushbutton_infoServer_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton_infoServer (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
function pushbutton_Query_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton_Query (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
[X,map] = imread('Search.png','BackgroundColor',[0.925 0.914 0.847]);
set(hObject,'CData',X);



% --- Executes during object creation, after setting all properties.
function pushbutton_infoServer_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton_infoServer (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
[X,map] = imread('Info.png','BackgroundColor',[0.925 0.914 0.847]);
set(hObject,'CData',X);

function resetServerPanel(handles)

set(handles.popupmenu_Comm,'Visible','off');
set(handles.popupmenu_Comm,'Value',1);
set(handles.popupmenu_DSN,'Visible','off');
set(handles.popupmenu_DSN,'Value',1);
set(handles.pushbutton_infoServer,'Visible','off');
set(handles.text_ServerDesc,'Visible','off');
set(handles.text_ServerDesc,'String','');



% --- Executes on button press in pushbutton_infoServer.
function pushbutton_infoServer_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_infoServer (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
web(['http://www.dasregistry.org/showdetails.jsp?auto_id=' handles.serverURI],'-browser');


% --- Executes during object creation, after setting all properties.
function pushbutton_Reset_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton_Reset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
[X,map] = imread('Back.png','BackgroundColor',[0.925 0.914 0.847]);
set(hObject,'CData',X);



% --- Executes during object creation, after setting all properties.
function pushbutton_ClearDisplay_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton_ClearDisplay (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
[X,map] = imread('reload.png','BackgroundColor',[0.925 0.914 0.847]);
set(hObject,'CData',X);



% --- Executes on button press in pushbutton_SendBrowser.
function pushbutton_SendBrowser_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_SendBrowser (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if (isempty(get(handles.field_XMLDisp,'String')))
    return;
end

%fid = fopen(fullfile(pwd,'fooXML.xml'),'w');
tmpFile = tempname;
fid = fopen(tmpFile,'w');
fprintf(fid,'%s',handles.currentXML);
fclose(fid);
%web(['file:///' which('fooXML.xml')],'-browser');
web(['file:///' tmpFile],'-browser');

% --- Executes on button press in pushbutton_SendXMLFile.
function pushbutton_SendXMLFile_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_SendXMLFile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if (isempty(get(handles.field_XMLDisp,'String')))
    h = msgbox('DAS Server response: empty','Query not saved','warn');
    return;
end
    
[filename, pathname] = uiputfile({'*.xml','XML Files (*.xml)';'*.*', 'All Files'},'Save XML File',...
    strcat(pwd,'\newfile.xml'));

if ( (strcmp(num2str(filename),'0')) && (strcmp(num2str(pathname),'0')) )
    return
else

    fid = fopen(fullfile(pathname,filename),'w');
    fprintf(fid,'%s',handles.currentXML);
    %         for i=1:size(text,1)
    %             fprintf(fid,'%s',text(i,:));
    %         end

    fclose(fid);
end

%   try
%         assignin('base', 'DASquery_XML', get(handles.field_XMLDisp,'String'));
%         xmlStruct = xml_parseany(get(handles.field_XMLDisp,'String'));
%         assignin('base', 'DASquery_Struct', xmlStruct);
%   catch
%         h = msgbox('Problem parsing XML','Query not saved into workspace','warn');
%         return;
%     end

% --- Executes during object creation, after setting all properties.
function pushbutton_SendBrowser_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton_SendBrowser (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
[X,map] = imread('connect.png','BackgroundColor',[0.925 0.914 0.847]);
set(hObject,'CData',X);



% --- Executes during object creation, after setting all properties.
function pushbutton_SendXMLFile_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton_SendXMLFile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
[X,map] = imread('save.png','BackgroundColor',[0.925 0.914 0.847]);
set(hObject,'CData',X);

function populateSegDefMenu(handles)
set(handles.popupmenu_SegDef,'String',['Choose data type...' handles.serverCoordinates]);
set(handles.popupmenu_SegDef,'Value',1);


% --- Executes on button press in pushbutton_infoCS.
function pushbutton_infoCS_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_infoCS (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
web(get(hObject,'TooltipString'),'-browser');


% --- Executes during object creation, after setting all properties.
function pushbutton_infoCS_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton_infoCS (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
[X,map] = imread('info16x16.png','BackgroundColor',[0.925 0.914 0.847]);
set(hObject,'CData',X);

function configureInfoCSButton(handles,panel)

if (any(strcmpi('entry_points',handles.serverCapab)) && strcmp(panel,'DNA Panel') )
    set(handles.pushbutton_infoCS,'TooltipString',strcat(handles.baseUrl,handles.dsn,'/entry_points?'));
elseif (strcmp(panel,'DNA Panel'))
    ind = get(handles.popupmenu_SegDef,'Value');
    set(handles.pushbutton_infoCS,'TooltipString',handles.serverCoordinatesURI{ind-1});
elseif (strcmp(panel,'ID Panel'))
    ind = get(handles.popupmenu_SegDef,'Value');
    set(handles.pushbutton_infoCS2,'TooltipString',handles.serverCoordinatesURI{ind-1});
end

    
% --- Executes on button press in pushbutton_infoCS2.
function pushbutton_infoCS2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_infoCS2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
web(get(hObject,'TooltipString'),'-browser');

% --- Executes during object creation, after setting all properties.
function pushbutton_infoCS2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton_infoCS2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
[X,map] = imread('info16x16.png','BackgroundColor',[0.925 0.914 0.847]);
set(hObject,'CData',X);

% --- Executes when figure1 is resized.
function figure1_ResizeFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

function resizeButtonImage(hButton,width,height)

set(hButton,'Units','Pixels');
pos = get(hButton,'Position');
set(hButton,'Position',[pos(1) pos(2) width height]); %set original pixel size


% --- Executes on button press in pushbutton_getURL.
function pushbutton_getURL_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_getURL (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

str = sprintf('[xmlS, xml, status, url, xmlS_2] = executeDASCommand(\''null\'',\''queryURL\'',\''%s\'');',get(handles.field_queryURL,'String'));
assignin('base', 'DASquery_APIcall', str);

% --- Executes during object creation, after setting all properties.
function pushbutton_getURL_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton_getURL (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
[X,map] = imread('url.png','BackgroundColor',[0.925 0.914 0.847]);
set(hObject,'CData',X);



% --- Executes during object creation, after setting all properties.
function uipanel_XMLDisp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to uipanel_XMLDisp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes when uipanel_DasClient is resized.
function uipanel_DasClient_ResizeFcn(hObject, eventdata, handles)
% hObject    handle to uipanel_DasClient (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton_Query.
function pushbutton_Query_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton_Query (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on key press with focus on pushbutton_Query and none of its controls.
function pushbutton_Query_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton_Query (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)





function field_queryURL_Callback(hObject, eventdata, handles)
% hObject    handle to field_queryURL (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of field_queryURL as text
%        str2double(get(hObject,'String')) returns contents of field_queryURL as a double


% --- Executes during object creation, after setting all properties.
function field_queryURL_CreateFcn(hObject, eventdata, handles)
% hObject    handle to field_queryURL (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function setResultsFields(hObject,handles,xml,url)

set(handles.field_XMLDisp,'String',xml);
handles.currentXML = xml;
set(handles.field_queryURL,'String',url);
handles.currentURL = url;
guidata(hObject,handles);



% --- Executes during object creation, after setting all properties.
function dasMiner_logo_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dasMiner_logo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate dasMiner_logo
%axes(findobj('Tag','lala'))
axes(hObject)
[X,map] = imread('logo.png');
imagesc(X)
axis(hObject,'off');



% --- Executes on mouse press over axes background.
function dasMiner_logo_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to dasMiner_logo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


